package com.life.muyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.life.muyu.dto.MailRecordsDTO;
import com.life.muyu.mapper.MailSendRecordsMapper;
import com.life.muyu.mapper.MailSubscriberMapper;
import com.life.muyu.mapper.NewsMapper;
import com.life.muyu.model.MailSendRecord;
import com.life.muyu.model.MailSubscriber;
import com.life.muyu.model.News;
import com.life.muyu.service.ExpressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zyb
 * @Date 2020-05-24 4:36 PM
 **/
@Service
public class ExpressServiceImpl extends ServiceImpl<MailSubscriberMapper, MailSubscriber> implements ExpressService {

    @Autowired
    private MailSubscriberMapper mailSubscriberMapper;

    @Autowired
    private MailSendRecordsMapper mailSendRecordsMapper;

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public List<MailRecordsDTO> getMailSendRecords() {
        return mailSendRecordsMapper.selectList(null).stream().map(record -> {
            for (MailSubscriber mailSubscriber : mailSubscriberMapper.selectList(null)) {
                if (record.getMailSender().equals(mailSubscriber.getId())) {
                    return MailRecordsDTO.builder()
                            .id(record.getId())
                            .mailAddr(mailSubscriber.getMailSubscriberAddr())
                            .sendTime(record.getSendTime())
                            .news(newsMapper.selectOne(new QueryWrapper<News>().eq("id", record.getNewsId())))
                            .build();
                }
            }
            return null;
        }).collect(Collectors.toList());
    }
}
